<template>
  <!-- 置标创建页面 -->
  <div class="Integration">
    <div class="btn-continer">
      <el-button type="primary" class="btn-lanse" @click="goEdit(1)"
        ><i class="el-icon-plus el-icon--left">新建</i></el-button
      >
      <el-button type="success" class="btn-lvse" @click="goEdit(2)"
        ><i class="el-icon-edit el-icon--left"> 编辑</i></el-button
      >
      <el-button type="danger" class="btn-hongse" @click="delFn"
        ><i class="el-icon-delete el-icon--left"> 删除</i></el-button
      >
      <div class="btn-right">
        <el-select
          v-model="value"
          placeholder="请选择"
          @change="refreshfn"
          style="margin-right: 10px"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-button
          icon="el-icon-refresh-right"
          @click="refreshfn()"
          circle
        ></el-button>
      </div>
    </div>
    <div class="table-continer">
      <el-table
        :data="tableData"
        v-loading="loading"
        :key="key"
        :highlight-current-row="true"
        :header-cell-style="{ background: '#F0F0F6', color: '#2D3548' }"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        row-key="id"
        lazy
        :load="getChildrens"
        style="width: 100%"
        @row-click="selectionClick"
        ref="multipleTable"
      >
        <el-table-column fixed label="名称">
          <template slot-scope="scope">
            <span style="height: 16px; display: inline-block"
              ><img
                src="../assets/bao.png"
                alt=""
                style="margin-top: 3px; display: block"
            /></span>
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column prop="valueType" label="类型"> </el-table-column>
        <el-table-column prop="creator" label="创建人"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间"> </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.state == 0">暂存</span>
            <span v-if="scope.row.state == 1">待审核</span>
            <span v-if="scope.row.state == 2">审核通过</span>
            <span v-if="scope.row.state == 3">否决</span>
            <span v-if="scope.row.state == 4">删除待审核</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              ><i class="el-icon-edit el-icon--left"> 编辑</i></el-button
            >
            <el-button type="text" size="small"
              ><i class="el-icon-view el-icon--left"> 预览</i></el-button
            >
          </template>
        </el-table-column> -->
      </el-table>
    </div>
    <div class="Pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="pageNum"
        :page-sizes="[5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <confim :dialogVisible="dialogVisible" @submitFn="submitFn" />
  </div>
</template>

<script>
import confim from "../components/confim.vue";
import { mapState } from "vuex";
export default {
  name: "Integration",
  components: { confim },

  data() {
    return {
      checkAll: false,
      dialogVisible: false,
      tableData: [],
      tableAllData: [], // 存储所有的表格数据
      loading: false,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      selectId: [],
      key: 0,
      value: "",
      options: [
        { value: "", label: "全部" },
        { value: 0, label: "暂存" },
        { value: 1, label: "待审核" },
        { value: 2, label: "审核通过" },
        { value: 3, label: "否决" },
        { value: 4, label: "删除待审核" },
      ],
    };
  },
  mounted() {
    this.pageNum = 1;
    this.getList();
  },
  methods: {
    refreshfn() {
      this.pageNum = 1;
      this.getList();
    },

    getList() {
      let obj = {
        parentId: 0,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        state: this.value,
      };
      this.loading = true;
      this.$api
        .TwoselectList(obj)
        .then((res) => {
          this.loading = false;
          this.tableData = res.data.list;
          this.tableData.forEach((res) => {
            res.hasChildren = true;
          });
          this.total = res.data.total;
          this.key = Math.random();
          this.selectId = [];
        })
        .catch(() => {
          this.selectId = [];
          this.loading = false;
        });
    },
    goEdit(id) {
      if (id == 2) {
        //编辑
        if (this.selectId.length == 1) {
          this.$router.push({
            path: "/IntegrationEdit",
            query: {
              id: id,
              parentId: this.selectId[0].id,
              creator: this.selectId[0].creator,
              row: this.selectId[0],
              title: "编辑信息",
            },
          });
        } else {
          this.$message.closeAll();
          this.$message({
            showClose: true,
            message: "请选择一条标签进行编辑！",
            type: "warning",
          });
        }
      } else {
        //新建
        if (this.selectId.length == 1) {
          this.$router.push({
            path: "/IntegrationEdit",
            query: {
              id: id,
              parentId: this.selectId[0].id,
              userName: sessionStorage["userName"],
              // creator: this.selectId[0].creator,
              title: `新建${this.selectId[0].name}子集`,
            },
          });
        } else if (this.selectId.length == 0) {
          this.$router.push({
            path: "/IntegrationEdit",
            query: {
              id: id,
              parentId: 0,
              userName: sessionStorage["userName"],
              // creator: this.selectId[0].creator,
              title: "新建",
            },
          });
        } else {
          this.$message.closeAll();
          this.$message({
            showClose: true,
            message: "请选择一条信息创建下一级标签",
            type: "warning",
          });
        }
      }
    },
    delFn() {
      if (this.selectId.length == 0) {
        this.$message({
          showClose: true,
          message: "请选择一条数据进行删除",
          type: "warning",
        });
      } else {
        if (this.selectId[0].state == 2) {
          this.$message({
            showClose: true,
            message: "审核通过，暂不予许删除",
            type: "warning",
          });
          return;
        }
        this.dialogVisible = true;
      }
    },
    getChildrens(tree, treeNode, resolve) {
      let obj = {
        parentId: tree.id,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
      this.$api.TwoselectList(obj).then((res) => {
        let data;
        if (Array.isArray([res.data])) {
          if (res.data.length == 0) {
            tree.hasChildren = false;
            this.$set(this.tableData, tree.id.toString() + "asd", "");
            return;
          }
        }
        if (res.data && res.data.list.length > 0) {
          data = res.data.list.map((item, index) => {
            return {
              ...item,
              hasChildren: true,
              children: [],
            };
          });
          this.tableAllData = this.tableAllData.concat(data); // 保存表格数据
          resolve(data);
        } else {
          tree.hasChildren = false;
          this.$set(this.tableData, tree.id.toString() + "asd", "");
        }
      });
    },

    // 删除回调
    submitFn(flag) {
      if (flag) {
        let obj = {
          id: this.selectId[0].id,
          state: this.selectId[0].state ? this.selectId[0].state : 0,
          user: window.sessionStorage["userName"],
          creator: this.selectId[0].creator,
        };
        this.$api
          .deleteLalbe(obj)
          .then((res) => {
            if (res.code == 200) {
              this.$message.closeAll();
              this.$message({
                showClose: true,
                message: res.msg,
                type: "success",
              });
              this.getList();
            }
          })
          .catch((res) => {
            this.$message({
              showClose: true,
              message: res.msg,
              type: "warning",
            });
          });

        this.dialogVisible = false;
      } else {
        this.dialogVisible = false;
      }
    },
    selectionClick(row) {
      this.selectId = [row];
    },
    // 全选
    // selectAll() {
    //   this.checkAll = !this.checkAll;
    //   this.tableAllData.map((row) => {
    //     console.log(row);
    //     if (Object.prototype.toString.call(row) === "[object Object]") {
    //       this.$refs.multipleTable.toggleRowSelection(row, this.checkAll);
    //     }
    //   });
    // },
    handleSizeChange(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getList();
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getList();
      // console.log(`当前页: ${val}`);
    },
  },
};
</script>
<style scoped lang="scss">
.Integration {
  padding: 0 30px;
  overflow: hidden;
  .btn-continer {
    margin-top: 30px;
    margin-bottom: 20px;
    .btn-right {
      float: right;
    }
  }
  .Pagination {
    text-align: right;
    margin-top: 40px;
    margin-bottom: 40px;
  }
}
</style>

